/*
 * Copyright© 2003-2019 浙江汇信科技有限公司, All Rights Reserved.
 */
package com.icinfo.cloud.provider.punish.ucase.casemain.service.impl;

import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.enums.RecordSaveTypeEnum;
import com.icinfo.cloud.provider.common.enums.YesOrNoEnum;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.common.utils.BaseUtil;
import com.icinfo.cloud.provider.punish.common.electricsent.model.DocumentSentRegister;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseReasonMainRecordQueryDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseReasonMainRecordSaveDto;
import com.icinfo.cloud.provider.punish.ucase.casemain.mapper.CaseReasonMainRecordMapper;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseReasonMainRecord;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.ICaseReasonMainRecordService;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseReasonMainRecordVo;
import com.icinfo.framework.common.web.Result;
import com.icinfo.framework.core.service.support.MyBatisServiceSupport;
import com.icinfo.framework.mybatis.mapper.entity.Example;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * 描述: 案件案由记录表 case_reason_main_record 对应的Service接口实现类.<br>
 *
 * @author framework generator
 * @date 2022年02月22日
 */
@Service
@Slf4j

public class CaseReasonMainRecordServiceImpl extends MyBatisServiceSupport implements ICaseReasonMainRecordService {
    @Resource
    private CaseReasonMainRecordMapper caseReasonMainRecordMapper;
    @Resource
    private CaseMainInfoServiceImpl caseMainInfoService;

    /**
     * 【V1.0.0】描述：保存案件案由记录
     *
     * @param saveDto {@link CaseReasonMainRecordSaveDto}
     * @return {@link Result <String>} 保存结果
     * @author xiangyuyu
     * @date 2022-02-28 10:47
     */
    @Override
    public Result<String> saveCaseReasonRecord(CaseReasonMainRecordSaveDto saveDto) {
        CaseReasonMainRecord caseReasonMainRecord = new CaseReasonMainRecord();
        if (!ObjectUtils.isEmpty(saveDto)) {
            BeanUtils.copyProperties(saveDto, caseReasonMainRecord);
        }
        String recordSaveType = saveDto.getOperateType();
        String id;
        try {
            if (RecordSaveTypeEnum.MOD.getCode().equals(recordSaveType)) {
                id = saveDto.getId();
                if (ObjectUtils.isEmpty(id)) {
                    return Result.error(Constant.STRING_NO_PRIMARY_KEY);
                } else {
                    CaseReasonMainRecordVo vo = getCaseReasonRecord(id);
                    if (ObjectUtils.isEmpty(vo)) {
                        return Result.error("未匹配到案件案由记录");
                    }
                    Example updateE = new Example(DocumentSentRegister.class);

                    updateE.createCriteria().andEqualTo("id", id);
                    BeanUtils.copyProperties(saveDto, caseReasonMainRecord);
                    caseReasonMainRecordMapper.updateByExampleSelective(caseReasonMainRecord, updateE);
                }
            } else {
                id = BaseUtil.createUid();
                caseReasonMainRecord.setId(id);
                caseReasonMainRecordMapper.insertSelective(caseReasonMainRecord);
            }
        } catch (Exception e) {
            e.printStackTrace();
            if (e.getMessage().contains(Constant.STRING_DUPLICATE_ENTRY) ||
                    e.getMessage().contains(Constant.STRING_DUPLICATE_ENTRY_LOWER)) {
                throw new BusinessException("请勿重复添加");
            } else {
                throw e;
            }
        }
        return Result.success(Constant.STRING_SAVE_SUCCESS, id);
    }

    /**
     * 【V1.0.0】描述：获取案件案由列表
     *
     * @param queryDto {@link CaseReasonMainRecordQueryDto}
     * @return {@link List <  CaseReasonMainRecordVo  >} 案件案由列表
     * @author xiangyuyu
     * @date 2022-02-28 10:47
     */
    @Override
    public List<CaseReasonMainRecordVo> getCaseReasonRecordList(CaseReasonMainRecordQueryDto queryDto) {
        String status = queryDto.getStatus();
        if(ObjectUtils.isEmpty(status)){
            queryDto.setStatus(YesOrNoEnum.YES.getCode());
        }
        List<CaseReasonMainRecordVo> caseReasonRecordList = caseReasonMainRecordMapper.selectCaseReasonRecordList(queryDto);
        return caseReasonRecordList;
    }

    /**
     * 【V1.0.0】描述：根据唯一标识查询案件案由记录
     *
     * @param id 唯一标识
     * @return {@link CaseReasonMainRecordVo} 案件案由记录
     * @author xiangyuyu
     * @date 2022-02-28 10:47
     */
    @Override
    public CaseReasonMainRecordVo getCaseReasonRecord(String id) {
        CaseReasonMainRecord caseReasonMainRecord = caseReasonMainRecordMapper.selectByPrimaryKey(id);
        if (ObjectUtils.isEmpty(caseReasonMainRecord)) {
            throw new BusinessException("未匹配到案件案由记录");
        }
        CaseReasonMainRecordVo vo = new CaseReasonMainRecordVo();
        BeanUtils.copyProperties(caseReasonMainRecord, vo);
        return vo;
    }

    /**
     * 描述：根据案件id获取案件案由详情数据【V2.0.0前仅适用单案由】
     *
     * @param caseId 案件id
     * @return {@link CaseReasonMainRecordVo} 案件案由详情数据
     * @author xiangyuyu
     * @date 2022-02-28 11:17
     */
    @Override
    @Deprecated
    public CaseReasonMainRecordVo getCaseReasonRecordByCaseId(String caseId) {
        Example queryExample = new Example(CaseReasonMainRecord.class);
        queryExample.createCriteria().andEqualTo("caseId", caseId).andEqualTo("status","1");
        List<CaseReasonMainRecord> caseReasonMainRecordList = caseReasonMainRecordMapper.selectByExample(queryExample);
        if (ObjectUtils.isEmpty(caseReasonMainRecordList) || caseReasonMainRecordList.size() == 0) {
            throw new BusinessException("未匹配到案件案由记录");
        }

        //fixme:目前只考虑案件单案由
        CaseReasonMainRecord record = caseReasonMainRecordList.get(Constant.INTEGER_0);
        CaseReasonMainRecordVo vo = new CaseReasonMainRecordVo();
        if (!ObjectUtils.isEmpty(record)) {
            BeanUtils.copyProperties(record, vo);
        }
        return vo;
    }

    /**
     * 描述：根据案件id获取案件案由详情数据-适配多案由
     *
     * @param caseId 案件id
     * @return {@link List<CaseReasonMainRecordVo>} 案件案由详情数据
     * @author xiangyuyu
     * @date 2022-05-23 09:20
     */
    @Override
    public List<CaseReasonMainRecordVo> getCaseReasonRecordListByCaseId(String caseId) {
        List<CaseReasonMainRecordVo> list = new ArrayList<>();
        Example queryExample = new Example(CaseReasonMainRecord.class);
        queryExample.createCriteria().andEqualTo("caseId", caseId).andEqualTo("status", "1");
        List<CaseReasonMainRecord> caseReasonMainRecordList = caseReasonMainRecordMapper.selectByExample(queryExample);
        // 注释掉是为了适配人设条线，人设条线在立案登记的时候不选择案由
        /*if (ObjectUtils.isEmpty(caseReasonMainRecordList) || caseReasonMainRecordList.size() == 0) {
            throw new BusinessException("未匹配到案件案由记录");
        }*/
        caseReasonMainRecordList.forEach(e -> {
            CaseReasonMainRecordVo vo = new CaseReasonMainRecordVo();
            if (!ObjectUtils.isEmpty(e)) {
                BeanUtils.copyProperties(e, vo);
                list.add(vo);
            }
        });
        return list;
    }

    /**
     * 清空处罚事项的违则罚则(速结转普通)
     * @param caseId
     */
    public void dealCaseReasonRecordOfChangeWorkFlow(String caseId) {
        Example queryExample = new Example(CaseReasonMainRecord.class);
        queryExample.createCriteria().andEqualTo("caseId", caseId).andEqualTo("status", "1");
        List<CaseReasonMainRecord> caseReasonMainRecordList = caseReasonMainRecordMapper.selectByExample(queryExample);
        if (ObjectUtils.isEmpty(caseReasonMainRecordList)) {
            return;
        }
        caseReasonMainRecordList.forEach(e -> {
            e.setIllegalBasis(null);
            e.setIllegalBasisFormat(null);
            e.setIllegalDetail(null);
            e.setIllegalDetailFormat(null);
            e.setPunishBasis(null);
            e.setPunishBasisFormat(null);
            e.setPunishDetail(null);
            e.setPunishDetailFormat(null);
            caseReasonMainRecordMapper.updateByPrimaryKey(e);
        });

    }
}